package com.qf.controller.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.domain.Address;
import com.qf.domain.Cart;
import com.qf.domain.Order;
import com.qf.domain.User;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/20 19:51
 * description:
 */
@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet{
    CartService cartService=new CartServiceImpl();
    AddressService addressService=new AddressServiceImpl();
    OrderService orderService=new OrderServiceImpl();







    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        String aid = request.getParameter("aid");
        Order order=orderService.addOrder(aid,user.getId());
        request.setAttribute("order",order);
        return "forward:/orderSuccess.jsp";

    }

    //预览 订单信息
    public String getOrderView(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        List<Cart> carts=cartService.findCartByUid(user.getId());
        List<Address> addList= addressService.findAddListByUid(user.getId());
        request.setAttribute("carts",carts);
        request.setAttribute("addList",addList);
        return "forward:/order.jsp";

    }
    // 支付成功
    public String paySuccess(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        try {
            String result = request.getParameter("result");//(json字符串)
            //把json字符串转换为json对象
            Map map = JSON.parseObject(result, Map.class);
            JSONObject jsonObject=(JSONObject) map.get("result");
            String result_code=(String) jsonObject.get("result_code");
            //如果支付成功，更新数据库订单状态
            if("success".equalsIgnoreCase(result_code)){
                //获取订单编号
                String orderNo=(String) jsonObject.get("out_trade_no");
                //更新订单状态
                Order order=new Order();
                order.setId(orderNo);
                order.setStatus("2");
                orderService.paySuccess(order);
            }
            request.setAttribute("msg","支付成功");
            return "forward:/message.jsp";
        } catch (Exception e) {
            request.setAttribute("msg","支付失败");
            return "forward:/message.jsp";
        }
    }
}
